package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;

    //批量添加预约设置
    @Override
    public void add(List<OrderSetting> list) {

        //判断一下集合是否为空
        if(list != null && list.size() > 0){
            //遍历list集合
            for (OrderSetting orderSetting : list) {
                //取出日期数据
                Date orderDate = orderSetting.getOrderDate();
                //去数据库查询看看是否有这条记录
                Long count = ordersettingDao.selectOrderDate(orderDate);

                //判断count是否大于0
                if(count > 0){
                    //说明以前是有数据的  此时我们需要更新数据
                    ordersettingDao.updateNumberByOrderDate(orderSetting);
                }else{
                    //没有我们插入数据
                    ordersettingDao.add(orderSetting);
                }
            }
        }


    }
    //获取批量添加数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //设值当月最大数和最小数
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        //调用dao层处理数据
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        //见数据重新存储到list集合
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            //将数据有重新封装到Map集合中
            Map orderSettingmap = new HashMap();
            //注意获取日期主体几号信息
            orderSettingmap.put("date",orderSetting.getOrderDate().getDate());

            orderSettingmap.put("number",orderSetting.getNumber());
            orderSettingmap.put("reservations",orderSetting.getReservations());
            System.out.println(orderSetting + "?????????");
            //将数据重新存入到list集合
            data.add(orderSettingmap);
        }
        return data;
    }
    //修改预约设置
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //取出数据
        Date orderDate = orderSetting.getOrderDate();
        //去数据库查询看看是否有这条记录
        Long count = ordersettingDao.selectOrderDate(orderDate);

        //判断count是否大于0
        if(count > 0){
            //说明以前是有数据的  此时我们需要更新数据
            ordersettingDao.updateNumberByOrderDate(orderSetting);
        }else{
            //没有我们插入数据
            ordersettingDao.add(orderSetting);
        }
    }
}
